<template>
  <div class="admin-dashboard">
    <!-- 数据概览 -->
    <el-row :gutter="20">
      <el-col :span="6" v-for="card in statsCards" :key="card.title">
        <el-card shadow="hover">
          <div class="stats-card">
            <div class="icon-wrapper" :style="{ backgroundColor: card.color }">
              <el-icon><component :is="card.icon" /></el-icon>
            </div>
            <div class="stats-info">
              <div class="title">{{ card.title }}</div>
              <div class="value">{{ card.value }}</div>
              <div class="trend" :class="{ 'up': card.trend > 0, 'down': card.trend < 0 }">
                {{ card.trend > 0 ? '+' : '' }}{{ card.trend }}% 较上周
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :span="16">
        <el-card class="chart-card">
          <template #header>
            <div class="chart-header">
              <span>志愿服务趋势</span>
              <el-radio-group v-model="timeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">全年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container">
            <v-chart class="chart" :option="serviceChartOption" autoresize />
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="chart-card">
          <template #header>
            <div class="chart-header">
              <span>项目分布</span>
            </div>
          </template>
          <div class="chart-container">
            <v-chart class="chart" :option="projectChartOption" autoresize />
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最新动态 -->
    <el-row :gutter="20" class="activity-row">
      <el-col :span="16">
        <el-card class="activity-card">
          <template #header>
            <div class="card-header">
              <span>最新动态</span>
              <el-button text type="primary">查看全部</el-button>
            </div>
          </template>
          <el-timeline>
            <el-timeline-item
              v-for="activity in activities"
              :key="activity.id"
              :timestamp="activity.time"
              :type="activity.type">
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="todo-card">
          <template #header>
            <div class="card-header">
              <span>待办事项</span>
              <el-button text type="primary">查看全部</el-button>
            </div>
          </template>
          <el-list>
            <el-list-item v-for="todo in todos" :key="todo.id">
              <template #prefix>
                <el-icon :class="todo.priority"><component :is="todo.icon" /></el-icon>
              </template>
              <div class="todo-item">
                <span class="content">{{ todo.content }}</span>
                <el-tag size="small" :type="todo.tagType">{{ todo.status }}</el-tag>
              </div>
            </el-list-item>
          </el-list>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { LineChart, PieChart } from 'echarts/charts'
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
} from 'echarts/components'
import VChart from 'vue-echarts'
import {
  User,
  Timer,
  List,
  Medal,
  Warning,
  InfoFilled,
  SuccessFilled
} from '@element-plus/icons-vue'

// 注册 ECharts 组件
use([
  CanvasRenderer,
  LineChart,
  PieChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
])

// 数据概览卡片
const statsCards = [
  {
    title: '总志愿者',
    value: '2,851',
    trend: 15,
    icon: 'User',
    color: '#409EFF'
  },
  {
    title: '总服务时长',
    value: '12,580',
    trend: 8,
    icon: 'Timer',
    color: '#67C23A'
  },
  {
    title: '进行中项目',
    value: '25',
    trend: -5,
    icon: 'List',
    color: '#E6A23C'
  },
  {
    title: '活跃团队',
    value: '36',
    trend: 12,
    icon: 'Medal',
    color: '#F56C6C'
  }
]

// 时间范围
const timeRange = ref('month')

// 服务趋势图表配置
const serviceChartOption = {
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['服务时长', '参与人数']
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  yAxis: [
    {
      type: 'value',
      name: '服务时长',
      position: 'left',
      axisLabel: {
        formatter: '{value} 小时'
      }
    },
    {
      type: 'value',
      name: '参与人数',
      position: 'right',
      axisLabel: {
        formatter: '{value} 人'
      }
    }
  ],
  series: [
    {
      name: '服务时长',
      type: 'line',
      smooth: true,
      data: [120, 132, 101, 134, 90, 230, 210],
      itemStyle: {
        color: '#409EFF'
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: 'rgba(64,158,255,0.2)'
            },
            {
              offset: 1,
              color: 'rgba(64,158,255,0)'
            }
          ]
        }
      }
    },
    {
      name: '参与人数',
      type: 'line',
      smooth: true,
      yAxisIndex: 1,
      data: [50, 82, 91, 84, 110, 130, 120],
      itemStyle: {
        color: '#67C23A'
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: 'rgba(103,194,58,0.2)'
            },
            {
              offset: 1,
              color: 'rgba(103,194,58,0)'
            }
          ]
        }
      }
    }
  ]
}

// 项目分布图表配置
const projectChartOption = {
  tooltip: {
    trigger: 'item',
    formatter: '{b}: {c} ({d}%)'
  },
  legend: {
    orient: 'vertical',
    left: 'left',
    padding: 5
  },
  series: [
    {
      type: 'pie',
      radius: ['40%', '70%'],
      avoidLabelOverlap: false,
      itemStyle: {
        borderRadius: 10,
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        show: false,
        position: 'center'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: 20,
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 35, name: '海洋保护', itemStyle: { color: '#409EFF' } },
        { value: 25, name: '环境教育', itemStyle: { color: '#67C23A' } },
        { value: 20, name: '科研调查', itemStyle: { color: '#E6A23C' } },
        { value: 15, name: '海滩清洁', itemStyle: { color: '#F56C6C' } },
        { value: 5, name: '其他', itemStyle: { color: '#909399' } }
      ]
    }
  ]
}

// 最新动态
const activities = [
  {
    id: 1,
    content: '新增志愿者 15 名',
    time: '2024-03-20 10:00',
    type: 'success'
  },
  // ... 更多动态
]

// 待办事项
const todos = [
  {
    id: 1,
    content: '审核新志愿者申请',
    status: '紧急',
    priority: 'high',
    icon: 'Warning',
    tagType: 'danger'
  },
  // ... 更多待办
]
</script>

<style scoped lang="scss">
.admin-dashboard {
  .stats-card {
    display: flex;
    align-items: center;
    padding: 20px;

    .icon-wrapper {
      width: 48px;
      height: 48px;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 16px;

      .el-icon {
        font-size: 24px;
        color: #fff;
      }
    }

    .stats-info {
      flex: 1;

      .title {
        font-size: 14px;
        color: #909399;
        margin-bottom: 8px;
      }

      .value {
        font-size: 24px;
        font-weight: bold;
        color: #303133;
        margin-bottom: 8px;
      }

      .trend {
        font-size: 12px;

        &.up {
          color: #67C23A;
        }

        &.down {
          color: #F56C6C;
        }
      }
    }
  }

  .chart-row {
    margin-top: 20px;
  }

  .chart-card {
    .chart-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .chart-container {
      height: 350px;

      .chart {
        height: 100%;
      }
    }
  }

  .activity-row {
    .activity-card,
    .todo-card {
      .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }

    .todo-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;

      .content {
        margin-right: 10px;
      }
    }
  }
}
</style> 